/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.engine.states;

import com.jme3.app.Application;
import com.jme3.app.state.AppStateManager;
import com.jme3.material.Material;
import com.jme3.material.RenderState;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.BatchNode;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

/**
 *
 * @author Carl
 */
public class CloudsAppState extends BaseAppState{

    public CloudsAppState(){
        
    }
    private float maximumCloudyDistance = 200;
    private float maximumMovementDistance = 40;
    private float cloudsY = 80;
    private BatchNode clouds = new BatchNode();
    private Vector3f windDirection;
    private float time;
    private float windChangeIntervall = 10;
    private float lastWindDirectionChange = 0;

    @Override
    public void initialize(AppStateManager stateManager, Application application){
        super.initialize(stateManager, application);
        Material materialCloud = new Material(mainApplication.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
        materialCloud.setColor("Color", new ColorRGBA(1, 1, 1, 0.8f));
        materialCloud.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
        for(int i=0;i<250;i++){
            Geometry cloud = new Geometry("", new Box(15, 4, 15));
            cloud.setMaterial(materialCloud);
            Vector3f location = new Vector3f(getRandomCloudPosition(), (cloudsY + (0.2f * i)), getRandomCloudPosition());
            cloud.setLocalTranslation(location);
            clouds.attachChild(cloud);
        }
        clouds.batch();
        clouds.setQueueBucket(RenderQueue.Bucket.Transparent);
        mainApplication.getRootNode().attachChild(clouds);
        windDirection = getRandomWindDirection();
    }

    @Override
    public void cleanup(){
        super.cleanup();
        mainApplication.getRootNode().detachChild(clouds);
    }

    @Override
    public void update(float lastTimePerFrame){
        time += lastTimePerFrame;
        if((time - lastWindDirectionChange) >= windChangeIntervall){
            windDirection = getRandomWindDirection();
            lastWindDirectionChange = time;
        }
        clouds.move(windDirection.mult(lastTimePerFrame));
        if(isOutOfBounds()){
            windDirection.negateLocal();
        }
    }
    
    private boolean isOutOfBounds(){
        if((clouds.getLocalTranslation().getX() < (-1 * maximumMovementDistance)) || (clouds.getLocalTranslation().getZ() < (-1 * maximumMovementDistance))){
            return true;
        }
        if(clouds.getLocalTranslation().length() > (2 * maximumMovementDistance)){
            return true;
        }
        return false;
    }
    
    private float getRandomCloudPosition(){
        return (float) (((Math.random() * 4) - 1) * maximumCloudyDistance);
    }
    
    private Vector3f getRandomWindDirection(){
        return new Vector3f(getRandomWindForce(), 0, getRandomWindForce());
    }
    
    private float getRandomWindForce(){
        return (float) ((Math.random() - 0.5f) * 30);
    }
}
